package et.six.eta.View.ViewPager;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Paint;

public class ViewModel {
    Bitmap bitmap;
    Paint paint;
    Matrix matrix;

    /***
     * 初始，固定的宽，高 left，top
     * */

    float defaultWidth;
    float defaultHeight;
    float defaultLeft;
    float defaultTop;

    /***
     * 当前的宽高，left，top 会随偏移量进行变化
     * */

    float currentWidth;
    float currentHeight;
    float currentLeft;
    float currentTop;

    public ViewModel(Context context,int resId) {

        bitmap= BitmapFactory.decodeResource(context.getResources(),resId);
        paint=new Paint();
        paint.setAlpha(0);
        matrix=new Matrix();
    }

    public ViewModel alpha(int alpha){
        paint.setAlpha(alpha);
        return this;
    }

    public ViewModel Width(float width){
        this.currentWidth=this.defaultWidth=width;
        return this;
    }

    public ViewModel Height(float height){
        this.currentHeight=this.defaultHeight=height;
        return this;
    }

    public ViewModel Left(float left){

       this.currentLeft=this.defaultLeft=left;
        return  this;
    }

    public ViewModel Top(float top){
        this.currentTop=defaultTop=top;
        return this;
    }
public  ViewModel create(){
        //如果没传高度
        if(defaultHeight<=0){
           if(defaultWidth>0){
                //有宽度，则按宽度的比例缩放
               currentHeight=defaultHeight=(defaultWidth/bitmap.getWidth())*bitmap.getHeight();
           }else {
               //没有宽度，则去图片的默认高度
            currentHeight=defaultHeight=bitmap.getHeight();
           }
        }
        //如果没传宽度
        if(defaultWidth<=0){
            if(defaultHeight>0){
                //有高度则按高度的比例缩放
                currentWidth=defaultWidth=(defaultHeight/bitmap.getHeight())*bitmap.getWidth();
            }else {
                //没有高度，则去图片的默认宽度
                currentWidth=defaultWidth=bitmap.getWidth();
            }
        }

        /***
         * 缩放图片比例到合适的尺寸
         * */
        matrix.postScale(currentWidth/bitmap.getWidth(),currentHeight/bitmap.getHeight());
        bitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        return this;
}
/**
 * @desc 当前宽度变化比例
 * **/
 public float widthRate(){
     return currentWidth/defaultWidth;
 }

 /***
  * 当前高度变化比例
  * */
 public float heightRate(){
     return currentHeight/defaultHeight;
 }
}
